package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserApplyBean;
import com.zfjt.packnum.PackNum00005;
import com.zfjt.service.UserApplyService;

/**
 * 注册用户列表接口（分页）
 * @author xk
 * @version 1.0
 * @date 2017年3月29日
 */
@Controller
public class UserApplyListController {
	
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
	
	//数据库查询的数据集
	private List<UserApplyBean> mData;
	
	//返回的数据集
	private List<UserApplyBean> mFhData;
	
	//当前页面
	private int mPage;
	
	//一页的数据个数
	private int mPageSize;
	
	@Autowired
	private UserApplyService UserApply;
	
	@RequestMapping(value="00005")
	@ResponseBody
	public ReturnBean Global(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		mData = new ArrayList<>();
		mFhData = new ArrayList<>();
		mData = UserApply.selectAll();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00005 num00005 = JSON.parseObject(data,PackNum00005.class);     
		mPage = Integer.parseInt(num00005.getPage());
		mPageSize = Integer.parseInt(num00005.getPagesize());
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			int num = mData.size();
			if(mPageSize >=  num){
				for (int i = num-1; i > -1; i--) {
					mFhData.add(mData.get(i));				
				}
			}else{
				for (int i = 0; i < mPageSize; i++) {
					int index = num - (mPage - 1)*mPageSize - i - 1;
					if (index >= 0) {
						mFhData.add(mData.get((num - (mPage - 1)*mPageSize - i - 1)));						
					}
				}	
			}
			
			if(mPageSize > mFhData.size()){
				mReturn.setCode("003");
			}else if(mPageSize == mFhData.size()){
				mReturn.setCode("000");
			}
			mReturn.setPortnum("00005");
			mReturn.setData(JSON.toJSONString(mFhData));				
		}
		return mReturn;
	}
}
